package com.system.honour.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.system.honour.entity.SysPermission;
import com.system.honour.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.SimpleIdGenerator;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:    自定义用户登录信息
 * @Author:         Joe
 * @CreateDate:     2020/4/4 14:12
 */
@Data
public class LoginUser extends SysUser implements UserDetails{

	private List<SysPermission> permissions;

	/**
	 * 已经授权的权限
	 * @return
	 */
	@Override
	@JsonIgnore
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return permissions.parallelStream().filter(p ->!StringUtils.isEmpty(p.getPermission()))
				.map(p -> new SimpleGrantedAuthority(p.getPermission())).collect(Collectors.toSet());
	}

	/**
	 * 是否检测账户过期
	 * @return
	 */
	@Override
	@JsonIgnore
	public boolean isAccountNonExpired() {
		return true;
	}

	/**
	 * 检测用户是否被锁定
	 * @return
	 */
	@Override
	public boolean isAccountNonLocked() {
		return getStatus()!= 2;
	}

	/**
	 * 是否检测账户身份过期
	 * @return
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
	/**
	 * 是否账户是否激活
	 * @return
	 */
	@Override
	public boolean isEnabled() {
		return true;
	}
}
